package com.ludaxu.google.fragment;

import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.ludaxu.google.R;
import com.ludaxu.google.adapter.DefaultAdapter;
import com.ludaxu.google.domain.AppInfo;
import com.ludaxu.google.domain.SubjectInfo;
import com.ludaxu.google.holder.BaseHolder;
import com.ludaxu.google.protocol.HomeProtocol;
import com.ludaxu.google.protocol.SubjectProtocol;
import com.ludaxu.google.tools.BitmapHelper;
import com.ludaxu.google.tools.UiUtils;
import com.ludaxu.google.view.LoadingPage.loadResult;

public class SubjectFragment extends BaseFragment {

	private List<SubjectInfo> datas;

	@Override
	public View createSuccessView() {
		ListView listView = new ListView(UiUtils.getContext());

		listView.setAdapter(new SubjectAdapter(datas));
		return listView;
	}

	public class SubjectAdapter extends DefaultAdapter<SubjectInfo> {

		public SubjectAdapter(List<SubjectInfo> datas) {
			super(datas);
		}

		@Override
		protected BaseHolder<SubjectInfo> getHolder() {
			return new SubjectHolder();
		}

		@Override
		protected List<SubjectInfo> onLoad() {
			SubjectProtocol protocol = new SubjectProtocol();
			List<SubjectInfo> load = protocol.load(datas.size());
			datas.addAll(load);
			return load;
		}
	}

	static class SubjectHolder extends BaseHolder<SubjectInfo> {
		ImageView item_icon;
		TextView item_txt;

		@Override
		public View initView() {
			bitmapUtils = BitmapHelper.getBitmapUtils();
			View contentView = UiUtils.inflate(R.layout.item_subject);
			this.item_icon = (ImageView) contentView
					.findViewById(R.id.item_icon);
			this.item_txt = (TextView) contentView.findViewById(R.id.item_txt);
			contentView.setTag(this);
			return contentView;
		}

		@Override
		public void refreshView(SubjectInfo data) {

			this.item_txt.setText(data.getDes());
			bitmapUtils.display(this.item_icon,
					"http://127.0.0.1:8090/image?name=" + data.getUrl());
		}

	}

	@Override
	public loadResult load() {
		SubjectProtocol protocol = new SubjectProtocol();
		datas = protocol.load(0);
		return checkData(datas);
	}

}
